package com.qibo.cashier;

import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.hjq.toast.ToastUtils;
import com.qibo.baselib.api.BaseMainControl;
import com.qibo.baselib.base.BaseActivity;
import com.qibo.baselib.base.ColoredStatusBarActivity;
import com.xw.repo.XEditText;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.logging.Logger;

import okhttp3.Call;

/**
 * 登录
 */
public class LoginActivity extends ColoredStatusBarActivity{
    private static String Tag = LoginActivity.class.getName();
    private XEditText edit_qccount;
    private XEditText edit_password;
    private Button  btn_submit;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        edit_qccount = findViewById(R.id.edit_qccount);
        edit_password = findViewById(R.id.edit_password);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void initAction() {
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toLogin();

                switchActivity(MainActivity.class);
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }

    /***
     * 去登陆
     */
    private void toLogin(){
        String account = edit_qccount.getText().toString().trim();
        String password = edit_password.getText().toString().trim();
        if(null == account || account.length()<0){
            ToastUtils.show("请输入账号");
            return;
        }
        if(null == password || password.length()<0){
            ToastUtils.show("请输入密码");
            return;
        }
        MainControl.get_login(account, password, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Logger.getLogger(Tag+"异常="+e);
                ToastUtils.show("服务器异常："+e);
            }
            @Override
            public void onResponse(String response, int id) {
                Logger.getLogger(Tag+"数据="+response);
                if (null != response) {
                    try {
                        if(BaseMainControl.returVerify(mContext,response)){

                            switchActivity(MainActivity.class);
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        });

    }

}
